// 在 widget 树中向上查找最近的父级`Scaffold`  widget

// Scaffold scaffold = context.findAncestorWidgetOfExactType<Scaffold>();

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/// 泛型查找Widget

findWidget1<T extends Widget>(BuildContext context) {
  T value = context.findAncestorWidgetOfExactType<T>();
  return value;
}
// 查找父级最近的Scaffold对应的ScaffoldState对象
// ScaffoldState _state = context.findAncestorStateOfType<ScaffoldState>()!;

/// 泛型查找State
/// 一般如果想提供State获取方法  用of方法
findWidgetState<T extends State>(BuildContext context) {
  T value = context.findAncestorStateOfType<T>();
  return value;
}

/// State LifeCycle
///I/flutter ( 5436): initState
// I/flutter ( 5436): didChangeDependencies
// I/flutter ( 5436): build
/// I/flutter ( 5436): reassemble
// I/flutter ( 5436): didUpdateWidget
// I/flutter ( 5436): build
/// I/flutter ( 5436): reassemble
// I/flutter ( 5436): deactive
// I/flutter ( 5436): dispose

class StateWidgetLifecycle extends StatefulWidget {
  const StateWidgetLifecycle({Key key, this.initValue = 0});

  final int initValue;

  @override
  State<StatefulWidget> createState() => LifeCycleState();
}

class LifeCycleState extends State<StateWidgetLifecycle> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    //初始化状态
    _counter = widget.initValue;
    print("initState");
  }

  @override
  Widget build(BuildContext context) {
    print("build");
    return Scaffold(
      bottomNavigationBar: CupertinoButton(
        color: Colors.blueGrey,
        child: Text("AAA"),
      ),
      body: Center(
        child: TextButton(
          child: Text('$_counter'),
          //点击后计数器自增
          onPressed: () => setState(
            () => ++_counter,
          ),
        ),
      ),
    );
  }

  @override
  void didUpdateWidget(StateWidgetLifecycle oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("didUpdateWidget ");
  }

  @override
  void deactivate() {
    super.deactivate();
    print("deactivate");
  }

  @override
  void dispose() {
    super.dispose();
    print("dispose");
  }

  @override
  void reassemble() {
    super.reassemble();
    print("reassemble");
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("didChangeDependencies");
  }
}

/*

of 方法获取State
context 的 findAncestorStateOfType 找到State
context.findAncestorStateOfType<ScaffoldState>();

static ScaffoldState of(BuildContext context, { bool nullOk = false }) {
    assert(nullOk != null);
    assert(context != null);
    final ScaffoldState result = context.findAncestorStateOfType<ScaffoldState>();
    if (nullOk || result != null)
      return result;
    throw FlutterError.fromParts(<DiagnosticsNode>[
      ErrorSummary(
        'Scaffold.of() called with a context that does not contain a Scaffold.'
      ),
      ErrorDescription(
        'No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). '
        'This usually happens when the context provided is from the same StatefulWidget as that '
        'whose build function actually creates the Scaffold widget being sought.'
      ),
      ErrorHint(
        'There are several ways to avoid this problem. The simplest is to use a Builder to get a '
        'context that is "under" the Scaffold. For an example of this, please see the '
        'documentation for Scaffold.of():\n'
        '  https://api.flutter.dev/flutter/material/Scaffold/of.html'
      ),
      ErrorHint(
        'A more efficient solution is to split your build function into several widgets. This '
        'introduces a new context from which you can obtain the Scaffold. In this solution, '
        'you would have an outer widget that creates the Scaffold populated by instances of '
        'your new inner widgets, and then in these inner widgets you would use Scaffold.of().\n'
        'A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, '
        'then use the key.currentState property to obtain the ScaffoldState rather than '
        'using the Scaffold.of() function.'
      ),
      context.describeElement('The context used was')
    ]);
  }


 */
